#!/usr/local/bin/python
#-*- encoding:utf-8 -*- 
import codecs 
from whoosh.index import create_in  
from whoosh.fields import *  
from chinesetokenizer import ChineseAnalyzer
import random
from whoosh.index import open_dir
from whoosh import qparser
from whoosh.collectors import TimeLimitCollector, TimeLimit

def cmp(t1, t2):
    k1, v1  = t1;
    k2, v2 = t2;
    return len(v2) - len(v1);

def getScore(total, pos):
    bar = total/3;
    if pos < bar:
        return 3;
    elif pos < bar*2:
        return 2;
    else:
        return 1;
def computeDis(results, value):
    if results == None:
        print 'Wrong';
        return 0;
    setA = set([hit.fields()['appid'] for hit in results]);
    setB = set(value.keys());
    l = len(setA.intersection(setB));
    logc = sum(value.values());

    return (l,logc);

def parseQD(s):
    sl = s.split('|');
    di = {};
    for i in range(1, len(sl)):
        id, appID, fre =  sl[i].split(':');
        di[appID] = int(fre);
    return (sl[0], di);

ix = open_dir("IndexDir/titleIndex");
parser = qparser.QueryParser('content',ix.schema, group=qparser.syntax.OrGroup);
searcher = ix.searcher()   ;
with open('bm25_unmatch_statistical', 'w') as filter:
    logstatistical = open('finalunmatchLogStatistical', 'r');
    count = 0;
    for line in logstatistical:
        li = line.strip().split('\t', 1);
        
        key, value = parseQD(li[1]);
        print 'processing %s' % key;
        query = parser.parse(key.decode('utf-8'));
        try:
            c = searcher.collector(limit=10);
            tlc = TimeLimitCollector(c, timelimit=30);
            searcher.search_with_collector(query, tlc);
            results = tlc.results();
            dis,logc =  computeDis(results, value);
        except TimeLimit:
            dis = -1;
            logc = -1;
            count += 1;    
        filter.write('%f\t%d\t%s\n' %(dis, logc, li[1]));
    logstatistical.close();
    print 'COUNT: %d' % count;
